
using System;
using System.Collections.Generic;
using System.Linq;
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.Directory;
using Nop.Services.Events;

namespace Nop.Services.Directory
{
    /// <summary>
    /// Organization service
    /// </summary>
    public partial class OrganizationService : IOrganizationService
    {
        #region Constants

        /// <summary>
        /// Key for caching
        /// </summary>
        private const string ORGANIZATIONS_ALL_KEY = "Nop.organization.all";
        /// <summary>
        /// Key for caching
        /// </summary>
        /// <remarks>
        /// {0} : organization ID
        /// </remarks>
        private const string ORGANIZATION_BY_ID_KEY = "Nop.organization.id-{0}";
        /// <summary>
        /// Key pattern to clear cache
        /// </summary>
        private const string ORGANIZATIONS_PATTERN_KEY = "Nop.organization.";
        #endregion
        
        #region Fields
        private readonly IRepository<Organization> _organizationRepository;
        private readonly IEventPublisher _eventPublisher;
        private readonly ICacheManager _cacheManager;
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="cacheManager">Cache manager</param>
        /// <param name="organizationRepository">Organization repository</param>
        /// <param name="eventPublisher">Event published</param>
        public OrganizationService(ICacheManager cacheManager,
            IRepository<Organization> organizationRepository,
            IEventPublisher eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._organizationRepository = organizationRepository;
            this._eventPublisher = eventPublisher;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Gets all Organizations
        /// </summary>
        /// <returns>Organizations</returns>
        public virtual IList<Organization> GetAllOrganizations()
        {
            string key = ORGANIZATIONS_ALL_KEY;
            return _cacheManager.Get(key, () =>
            {
                var query = from a in _organizationRepository.Table
                            where !a.Deleted
                            orderby a.Id
                            select a;
                return query.ToList();
            });
        }

        /// <summary>
        /// Gets a Organization 
        /// </summary>
        /// <param name="organizationId">Organization identifier</param>
        /// <returns>Organization</returns>
        public virtual Organization GetOrganizationById(int organizationId)
        {
            if (organizationId == 0)
                return null;

            string key = string.Format(ORGANIZATION_BY_ID_KEY, organizationId);
            return _cacheManager.Get(key, () => _organizationRepository.GetById(organizationId));
        }

        /// <summary>
        /// Inserts a Organization
        /// </summary>
        /// <param name="organization">Organization</param>
        public virtual void InsertOrganization(Organization organization)
        {
            if (organization == null)
                throw new ArgumentNullException("organization");

            _organizationRepository.Insert(organization);

            _cacheManager.RemoveByPattern(ORGANIZATIONS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityInserted(organization);
        }

        /// <summary>
        /// Updates the Organization
        /// </summary>
        /// <param name="organization">Organization</param>
        public virtual void UpdateOrganization(Organization organization)
        {
            if (organization == null)
                throw new ArgumentNullException("organization");

            _organizationRepository.Update(organization);

            _cacheManager.RemoveByPattern(ORGANIZATIONS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityUpdated(organization);
        }

        /// <summary>
        /// Deletes a Organization
        /// </summary>
        /// <param name="organization">Organization</param>
        public virtual void DeleteOrganization(Organization organization)
        {
            if (organization == null)
                throw new ArgumentNullException("organization");

            _organizationRepository.Delete(organization);

            _cacheManager.RemoveByPattern(ORGANIZATIONS_PATTERN_KEY);

            //event notification
            _eventPublisher.EntityDeleted(organization);
        }

        #endregion
    }
}
